import DailyNotesMergePlugin from "./main";
import {Setting} from "obsidian";

export class JobDef {
  headingText: string = "";
  scanRangePath: string = "";
  outputPath: string = "";
  enabled: boolean = true;
}

export class GeneralUI {
    parent: HTMLElement;
    plugin: DailyNotesMergePlugin;

    constructor(parent: HTMLElement, plugin: DailyNotesMergePlugin) {
        this.parent = parent;
        this.plugin = plugin;
    }

    display() {
        const {parent} = this;
        /* General Settings */
        parent.createEl("h2", { text: "General Settings" });
        const desc3 = document.createDocumentFragment();
        desc3.append(
            "General Settings."
        );
        new Setting(parent).setDesc(desc3);

        new Setting(parent)
            .setName('Populate empty title')
            .setDesc('toggle to populate empty title.')
            .addToggle(toggle => toggle
                .setValue(this.plugin.settings.populateEmptyTitle)
                .onChange(async (value) => {
                        this.plugin.settings.populateEmptyTitle = value
                        await this.plugin.saveSettings()
                    }
                ));

        new Setting(parent)
            .setName('Make title links or not')
            .setDesc('toggle to make title links.')
            .addToggle(toggle => toggle
                .setValue(this.plugin.settings.titleLinks)
                .onChange(async (value) => {
                        this.plugin.settings.titleLinks = value
                        await this.plugin.saveSettings()
                    }
                ));
        /* end */
    }
}
export class CommonSingleJobUI {
  parent: HTMLElement;
  plugin: DailyNotesMergePlugin;

  constructor(parent: HTMLElement, plugin: DailyNotesMergePlugin) {
    this.parent = parent;
    this.plugin = plugin;
  }

  arraymove<T>(
      arr: T[],
      fromIndex: number,
      toIndex: number
  ): void {
    if (toIndex < 0 || toIndex === arr.length) {
      return;
    }
    const element = arr[fromIndex];
    arr[fromIndex] = arr[toIndex];
    arr[toIndex] = element;
  }

  display() {
    const {parent} = this;

    /* Single Jobs Setting */
    parent.createEl("h2", { text: "Execute Single Jobs" });
    const desc = document.createDocumentFragment();
    desc.append(
        "Execute Single Jobs by header, location, output and toggle enabled."
    );
    new Setting(parent).setDesc(desc);
    this.plugin.settings.jobs.forEach((job, index) => {
      const s = new Setting(parent)
          .addText(text => text
              .setPlaceholder("日常事项")
              .setValue(this.plugin.settings.jobs[index].headingText)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].headingText = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addText(text => text
              .setPlaceholder("1_Inbox/Diary/2023/2023-07")
              .setValue(this.plugin.settings.jobs[index].scanRangePath)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].scanRangePath = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addText(text => text
              .setPlaceholder("1_Inbox/Diary/output.md")
              .setValue(this.plugin.settings.jobs[index].outputPath)
              .onChange(async (value) => {
                    this.plugin.settings.jobs[index].outputPath = value;
                    await this.plugin.saveSettings();
                  }
              ))
          .addExtraButton((cb) => {
            cb.setIcon("up-chevron-glyph")
                .setTooltip("Move up")
                .onClick(async () => {
                  this.arraymove(
                      this.plugin.settings.jobs,
                      index,
                      index - 1
                  );
                  await this.plugin.saveSettings();
                  this.display();
                });
          })
          .addExtraButton((cb) => {
            cb.setIcon("down-chevron-glyph")
                .setTooltip("Move down")
                .onClick(async () => {
                  this.arraymove(
                      this.plugin.settings.jobs,
                      index,
                      index + 1
                  );
                  await this.plugin.saveSettings();
                  this.display();
                });
          })
          .addExtraButton((cb) => {
            cb.setIcon("cross")
                .setTooltip("Delete")
                .onClick(async () => {
                  this.plugin.settings.jobs.splice(
                      index,
                      1
                  );
                  await this.plugin.saveSettings();
                  // Force refresh
                  this.display();
                });
          })
          .addExtraButton(cb => {
            cb.setIcon("copy")
                .setTooltip("Copy")
                .onClick(async () =>{
                  let copiedJobDef = new JobDef();
                  copiedJobDef.headingText = this.plugin.settings.jobs[index].headingText;
                  copiedJobDef.scanRangePath = this.plugin.settings.jobs[index].scanRangePath;
                  copiedJobDef.outputPath = this.plugin.settings.jobs[index].outputPath;
                  copiedJobDef.enabled = true;
                  this.plugin.settings.jobs.push(copiedJobDef);
                  await this.plugin.saveSettings();
                  // Force refresh
                  this.display();
                })
          })
          .addExtraButton(cb => {
            cb.setIcon("play-circle")
                .setTooltip("Execute")
                .onClick(async () =>{
                  await this.plugin.executeSingeJob(this.plugin.settings.jobs[index], true);
                  // const content = await this.plugin.mergeDailyNotesHeading(this.plugin.settings.jobs[index].headingText,
                  //     this.plugin.settings.jobs[index].scanRangePath,
                  //     this.plugin.settings.includeSubHeadings,
                  //     true);
                  // this.plugin.writeToOutputPath(this.plugin.settings.jobs[index].outputPath, this.plugin.settings.jobs[index].headingText, content);
                })
          });

    });
    new Setting(parent).addButton((cb) => {
      cb.setButtonText("Add new job")
          .setCta()
          .onClick(async () => {
            this.plugin.settings.jobs.push(new JobDef());
            await this.plugin.saveSettings();
            // Force refresh
            this.display();
          });
    });
    new Setting(parent)
        .setName('Include sub-headings')
        .setDesc('toggle to include sub-headings.')
        .addToggle(toggle => toggle
            .setValue(this.plugin.settings.includeSubHeadings)
            .onChange(async (value) => {
                  this.plugin.settings.includeSubHeadings = value
                  await this.plugin.saveSettings()
                }
            ));
    /* end */
  }
}

export class CommonRecursivelyJobUI {
    parent: HTMLElement;
    plugin: DailyNotesMergePlugin;

    constructor(parentElement: HTMLElement, plugin: DailyNotesMergePlugin) {
        this.parent = parentElement;
        this.plugin = plugin;
    }
    arraymove<T>(
        arr: T[],
        fromIndex: number,
        toIndex: number
    ): void {
        if (toIndex < 0 || toIndex === arr.length) {
            return;
        }
        const element = arr[fromIndex];
        arr[fromIndex] = arr[toIndex];
        arr[toIndex] = element;
    }

    display() {
        const {parent} = this;
        /* Recursively Jobs Setting */
        parent.createEl("h2", { text: "Execute Recursively Jobs" });
        const desc2 = document.createDocumentFragment();
        desc2.append(
            "Execute Recursively Job by header, location, output and toggle enabled."
        );
        new Setting(parent).setDesc(desc2);

        this.plugin.settings.recursivelyJobs.forEach((job, index) => {
            const s = new Setting(parent)
                .addText(text => text
                    .setPlaceholder("日常事项")
                    .setValue(this.plugin.settings.recursivelyJobs[index].headingText)
                    .onChange(async (value) => {
                            this.plugin.settings.recursivelyJobs[index].headingText = value;
                            await this.plugin.saveSettings();
                        }
                    ))
                .addText(text => text
                    .setPlaceholder("1_Inbox/Diary/2023/2023-07")
                    .setValue(this.plugin.settings.recursivelyJobs[index].scanRangePath)
                    .onChange(async (value) => {
                            this.plugin.settings.recursivelyJobs[index].scanRangePath = value;
                            await this.plugin.saveSettings();
                        }
                    ))
                .addText(text => text
                    .setPlaceholder("1_Inbox/Diary/output.md")
                    .setValue(this.plugin.settings.recursivelyJobs[index].outputPath)
                    .onChange(async (value) => {
                            this.plugin.settings.recursivelyJobs[index].outputPath = value;
                            await this.plugin.saveSettings();
                        }
                    ))
                .addExtraButton((cb) => {
                    cb.setIcon("up-chevron-glyph")
                        .setTooltip("Move up")
                        .onClick(async () => {
                            this.arraymove(
                                this.plugin.settings.recursivelyJobs,
                                index,
                                index - 1
                            );
                            await this.plugin.saveSettings();
                            this.display();
                        });
                })
                .addExtraButton((cb) => {
                    cb.setIcon("down-chevron-glyph")
                        .setTooltip("Move down")
                        .onClick(async () => {
                            this.arraymove(
                                this.plugin.settings.recursivelyJobs,
                                index,
                                index + 1
                            );
                            await this.plugin.saveSettings();
                            this.display();
                        });
                })
                .addExtraButton((cb) => {
                    cb.setIcon("cross")
                        .setTooltip("Delete")
                        .onClick(async () => {
                            this.plugin.settings.recursivelyJobs.splice(
                                index,
                                1
                            );
                            await this.plugin.saveSettings();
                            // Force refresh
                            this.display();
                        });
                })
                .addExtraButton(cb => {
                    cb.setIcon("copy")
                        .setTooltip("Copy")
                        .onClick(async () =>{
                            let copiedJobDef = new JobDef();
                            copiedJobDef.headingText = this.plugin.settings.recursivelyJobs[index].headingText;
                            copiedJobDef.scanRangePath = this.plugin.settings.recursivelyJobs[index].scanRangePath;
                            copiedJobDef.outputPath = this.plugin.settings.recursivelyJobs[index].outputPath;
                            copiedJobDef.enabled = true;
                            this.plugin.settings.recursivelyJobs.push(copiedJobDef);
                            await this.plugin.saveSettings();
                            // Force refresh
                            this.display();
                        })
                })
                .addExtraButton(cb => {
                    cb.setIcon("play-circle")
                        .setTooltip("Execute")
                        .onClick(async () =>{
                            await this.plugin.executeRecursivelyJob(this.plugin.settings.recursivelyJobs[index], true);
                        })
                });

        });
        new Setting(parent).addButton((cb) => {
            cb.setButtonText("Add new job")
                .setCta()
                .onClick(async () => {
                    this.plugin.settings.recursivelyJobs.push(new JobDef());
                    await this.plugin.saveSettings();
                    // Force refresh
                    this.display();
                });
        });
        /* end */
    }
}